package com.kpx.app.view;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.kpx.app.R;
import com.kpx.app.utils.uiutils.DisplayUtil;
import com.kpx.app.utils.StringUtil;
import com.kpx.app.wigdet.ClearEditText;
import com.kpx.app.wigdet.LoadingDialog;


/**
 * 页面顶部的标题栏
 * Created by cc on 2016/6/14.
 */
public class TitleView extends LinearLayout implements View.OnClickListener {
    public TextView txt_title_right;
    protected TextView txt_title;
    protected LinearLayout toolbarMiddleView;
    protected TextView txt_sm_back;
    protected Context context;
    public LoadingDialog dialog;
    private LinearLayout rl_container;
    private ClearEditText et_search;
    private TextView txt_mg_count;
    private TextView txt_title_right2;
    private TextView txt_change_search;

    public TitleView(Context context) {
        super(context);
        initView(context);
    }

    public TitleView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView(context);
    }

    public TitleView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context);
    }

    /**
     * 初始化view
     *
     * @param context
     */
    private void initView(Context context) {
        this.context = context;
        View view = View.inflate(context, R.layout.view_title, this);
        rl_container = (LinearLayout) view.findViewById(R.id.rl_container);
        toolbarMiddleView = (LinearLayout) view.findViewById(R.id.middleTitleContainer);
        txt_title = ((TextView) view.findViewById(R.id.txt_title));
        txt_sm_back = (TextView) view.findViewById(R.id.txt_sm_back);
        et_search = (ClearEditText) view.findViewById(R.id.et_search);
        txt_title_right = (TextView) view.findViewById(R.id.txt_title_right);
        txt_title_right2 = (TextView) view.findViewById(R.id.txt_title_right2);
        txt_mg_count = (TextView) view.findViewById(R.id.txt_mg_count);
        txt_change_search = (TextView) view.findViewById(R.id.txt_change_search);
        txt_sm_back.setOnClickListener(this);
        txt_title_right.setOnClickListener(this);
        txt_title_right.setVisibility(View.GONE);
        View viewright = findViewById(R.id.ll_title_rightContainer);
        if (null != viewright) {
            viewright.setVisibility(View.INVISIBLE);
        }
        setMgCount(0);
    }

    public TextView getRightView() {
        return txt_title_right;
    }

    /**
     * 设置显示搜索框
     */
    public void setEt_searchShow() {
        txt_title.setVisibility(View.GONE);
        et_search.setVisibility(View.VISIBLE);
    }

    public void setMgCount(int count) {
        if (count <= 0) {
            txt_mg_count.setVisibility(View.GONE);
            txt_mg_count.setText("");
        } else {
            txt_mg_count.setVisibility(View.VISIBLE);
            txt_mg_count.setText((count > 99 ? "99+" : count) + "");
        }
    }

    /**
     * 添加搜索条件点击
     */
    public void addChangeSearchTxt(String search, OnClickListener listener) {
        txt_change_search.setVisibility(View.VISIBLE);
        et_search.setPadding(DisplayUtil.dip2px(context, 50), DisplayUtil.dip2px(context, 3), DisplayUtil.dip2px(context, 6), DisplayUtil.dip2px(context, 3));
        setTxtChangeTxt(search);
        setTxtChangetTxtClick(listener);
    }

    /**
     * 设置搜索条件点击事件
     *
     * @param listener
     */
    private void setTxtChangetTxtClick(OnClickListener listener) {
        txt_change_search.setOnClickListener(listener);
    }

    /**
     * 设置搜索条件
     *
     * @param search
     */
    public void setTxtChangeTxt(String search) {
        if (!TextUtils.isEmpty(search)) {
            txt_change_search.setText(search);
        }
    }

    /**
     * 获取输入框
     *
     * @return
     */
    public EditText getEt_search() {
        return et_search;
    }

    @Override
    public void onClick(View v) {

    }

    public void setTxt_title_right2(String txt2, int drawable, OnClickListener listener) {
        setTxt_title_right2_onclick(listener);
        setTxt_title_right2_drawable(drawable);
        setTxt_title_right2_txt(txt2);
    }

    public void setTxt_title_right2_txt(String txt2) {
        setTxt_title_right2_visiable(true);
        if (!TextUtils.isEmpty(txt2)) {
            txt_title_right2.setText(txt2);
        }
    }

    public void setTxt_title_right2_drawable(int iconId) {
        setTxt_title_right2_visiable(true);
        Drawable drawable = getResources().getDrawable(iconId);
        drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
        txt_title_right2.setCompoundDrawables(null, null, drawable, null);
    }

    public void setTxt_title_right2_onclick(OnClickListener listener) {
        txt_title_right2.setOnClickListener(listener);
    }

    public void setTxt_title_right2_visiable(boolean v) {
        txt_title_right2.setVisibility(v ? View.VISIBLE : View.GONE);
    }

    /**
     * 右边菜单是否显示
     */
    public void setRightIconVisible(boolean visialbe) {
        txt_title_right.setVisibility(visialbe ? View.VISIBLE : View.INVISIBLE);
        findViewById(R.id.ll_title_rightContainer).setVisibility(visialbe ? View.VISIBLE : View.INVISIBLE);
    }

    /**
     * 左边菜单是否显示
     */
    public void setLeftVisible(boolean visialbe) {
        findViewById(R.id.txt_sm_back).setVisibility(visialbe ? View.VISIBLE : View.INVISIBLE);
    }

    /**
     * 左边菜单是否显示
     */
    public void setLeftIconVisible(boolean visialbe) {
        if (!visialbe) {
            txt_sm_back.setCompoundDrawables(null, null, null, null);
        } else {
            Drawable drawable = getResources().getDrawable(R.drawable.return_btn);
            drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());// 这一步必须要做,否则不会显示.
            txt_sm_back.setCompoundDrawables(drawable, null, null, null);
        }
    }


    /**
     * 设置右侧菜单
     *
     * @param visialbe 是否可见
     * @param rightMenuText 菜单显示内容
     * @param listener 点击监听器
     */
    public void setRight(boolean visialbe, String rightMenuText, OnClickListener listener) {
        setRightIconVisible(visialbe);
        txt_title_right.setText(rightMenuText);
        txt_title_right.setTextSize(14);
        if (listener != null)
            txt_title_right.setOnClickListener(listener);
    }

    /**
     * 设置左侧菜单
     *
     * @param visialbe 是否可见
     * @param rightMenuText 菜单显示内容
     * @param listener 点击监听器
     */
    public void setLeft(boolean visialbe, String rightMenuText, OnClickListener listener) {
        setLeftVisible(visialbe);
        txt_sm_back.setText(rightMenuText);
        txt_sm_back.setTextSize(14);
        if (listener != null)
            txt_sm_back.setOnClickListener(listener);
    }

    /**
     * 设置右边点击事件
     *
     * @param listener
     */
    public void setRightClickListener(OnClickListener listener) {
        txt_title_right.setOnClickListener(listener);
    }

    /**
     * 设置右侧菜单
     *
     * @param rightMenuText 菜单显示内容
     * @param listener 点击监听器
     */
    public void setRight(String rightMenuText, OnClickListener listener) {
        setRight(true, rightMenuText, listener);
    }

    /**
     * 设置右侧菜单
     *
     * @param rightMenuText 菜单显示内容
     * @param listener 点击监听器
     */
    public void setRight(String rightMenuText, OnClickListener listener, int color) {
        setRight(true, rightMenuText, listener);
        txt_title_right.setTextColor(context.getResources().getColor(color));
    }

    /**
     * 设置左侧菜单
     *
     * @param leftMenuText 菜单显示内容
     * @param listener 点击监听器
     */
    public void setLeft(String leftMenuText, OnClickListener listener, int color, boolean isShowIcon) {
        setLeft(true, leftMenuText, listener);
        setLeftIconVisible(isShowIcon);
        txt_sm_back.setTextColor(context.getResources().getColor(color));
    }

    /**
     * 设置右侧菜单icon
     *
     * @param listener 点击监听器
     */
    public void setRight(int iconId, OnClickListener listener) {
        txt_title_right.setVisibility(View.VISIBLE);
        findViewById(R.id.ll_title_rightContainer).setVisibility(View.VISIBLE);
        txt_title_right.setText("");
        Drawable drawable = getResources().getDrawable(iconId);
        drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
        txt_title_right.setCompoundDrawables(null, null, drawable, null);
        txt_title_right.setOnClickListener(listener);
    }

    /**
     * 设置title左右的字体大小
     *
     * @param leftSize 左侧字体大小
     * @param rightSize 右侧字体大小
     * 给0为不设置
     */
    public void setTxtSize(int leftSize, int rightSize) {
        if (leftSize != 0) {
            txt_sm_back.setTextSize(leftSize);
        }
        if (rightSize != 0) {
            txt_title_right.setTextSize(rightSize);
        }
    }

    /**
     * 设置返回按钮的图标和文字
     *
     * @param navifationIconRes 返回按钮的图片资源ID（当小于0是不显示图标）
     */
    public void setLeftIconRes(int navifationIconRes) {
        if (navifationIconRes > 0) {
            Drawable drawableLeft = getResources().getDrawable(navifationIconRes);
            if (null != drawableLeft) {
                drawableLeft.setBounds(0, 0, drawableLeft.getMinimumWidth(), drawableLeft.getMinimumHeight());
                txt_sm_back.setCompoundDrawables(drawableLeft, null, null, null);
            }
        } else {
            txt_sm_back.setCompoundDrawables(null, null, null, null);
        }
    }

    /**
     * 设置返回文字
     */
    public void setLeftIconRes(String text) {
        txt_sm_back.setText(text);
    }

    /**
     * 设置左侧返回按钮返回事件
     */
    public void setBackFinish() {
        txt_sm_back.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                ((Activity) context).finish();
            }
        });
    }
    /**
     * 设置左侧返回按钮返回事件
     */
    public void setBackFinish(OnClickListener listener) {
        txt_sm_back.setOnClickListener(listener);
    }

    /**
     * 设置左侧返回按钮返回事件
     */
    public void setBackClickListener(OnClickListener listener) {
        txt_sm_back.setOnClickListener(listener);
    }

    /**
     * 设置返回文字颜色
     *
     * @param resId
     */
    public void setBackTxtColor(int resId) {
        txt_sm_back.setTextColor(resId);
    }

    /**
     * 设置Toolbat背景色
     *
     * @param resId
     */
    public void setTitleBgColor(int resId) {
        rl_container.setBackgroundColor(resId);
    }

    /**
     * 设置title文字颜色
     *
     * @param resId
     */
    public void setTitleColor(int resId) {
        txt_title.setTextColor(resId);
    }

    /**
     * 获取title中间的view
     *
     * @return
     */
    public TextView getTitle() {
        return txt_title;
    }

    /**
     * 设置返回按钮是否可见
     *
     * @param visible
     */
    public void setBackIconVisibility(boolean visible) {
        txt_sm_back.setVisibility(visible ? View.VISIBLE : View.INVISIBLE);
    }

    /**
     * 设置Toolbar标题是否可见
     */
    public void setTitleVisibility(int visibility) {
        txt_title.setVisibility(visibility);
    }

    /* 设置Toolbar自定义Tittle的内容* */
    public void setTitle(String middleTitle) {
        if (StringUtil.isEmpty(middleTitle)) {
            middleTitle = "";
        }
        txt_title.setText(middleTitle);
    }

    /* 设置Toolbar自定义Tittle的内容* */
    public void setTitle(String middleTitle, int drawableleft) {
        if (StringUtil.isEmpty(middleTitle)) {
            middleTitle = "";
        }
        Drawable drawableLeft = getResources().getDrawable(drawableleft);
        if (null != drawableLeft) {
            drawableLeft.setBounds(0, 0, drawableLeft.getMinimumWidth(), drawableLeft.getMinimumHeight());
            txt_title.setCompoundDrawablePadding(5);
            txt_title.setCompoundDrawables(drawableLeft, null, null, null);
        }
        txt_title.setText(middleTitle);
    }


    /* 设置Toolbar自定义Tittle的内容* */
    public void setTitle(String middleTitle, OnClickListener listener) {
        setTitle(middleTitle);
        setTitleOnClickListener(listener);
    }

    /**
     * 点击中间标题
     */
    public void setTitleOnClickListener(OnClickListener listener) {
        txt_title.setOnClickListener(listener);
    }

    /* 设置Toolbar自定义Tittle的内容* */
    public void setTitle(int middleTitleRes) {
        String middleTitle = context.getString(middleTitleRes);
        setTitle(middleTitle);
    }


    /**
     * 设置Toolbar中间视图是否可见
     */
    public void setCustomToolbarMiddleViewVisibility(int visibility) {
        toolbarMiddleView.setVisibility(visibility);
    }

    /**
     * 设置Toolbar是否可见
     */
    public void setToolbarVisibility(boolean visible) {
        if (visible) {
            setVisibility(View.VISIBLE);
        } else {
            setVisibility(View.GONE);
        }
    }


    /**
     * 设置Toolbar的高度
     *
     * @param dpSize
     */
    public void setTitleHeight(int dpSize) {
        if (null != rl_container) {
            LayoutParams layoutParams = new LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, (int) DisplayUtil.dip2px(context, dpSize));
            rl_container.setLayoutParams(layoutParams);
        }
    }
}
